<template>
  <div class="comment">
    <div class="putin">
      <div class="left">
        <h3>提交评论</h3>
      </div>
      <div class="right" @click="$router.back()">返回</div>
    </div>
    <van-field
      v-model="message"
      rows="1"
      autosize
      type="textarea"
      placeholder="请输入评论内容..."
    />
    <van-button type="info" @click="publish">发表</van-button>

    <div class="list">
      <div class="left">
        <h3>评论列表</h3>
      </div>
      <div class="right">{{ comments.length }}条评论</div>
    </div>
    <div class="box" v-for="item in comments" :key="item.content">
      <span class="content">{{ item.content }}</span>
      <div class="news">
        <div class="news_author">
          <p>{{ item.user_name }}</p>
          <span class="right">{{ item.add_time | dateFormat }}</span>
        </div>
      </div>
    </div>

    <van-button
      plain
      type="danger"
      style="width: 341 / 375 * 100vw; margin: 20px 16px; color: red"
      >加载更多</van-button
    >
  </div>
</template>

<script>
import { getCommentsInfo, publishComment } from "@/api/comment.js";
import { dateFormat } from "@/utils/filters.js";
export default {
  props: ["id"],
  data() {
    return {
      message: "",
      comments: [],
    };
  },
  async mounted() {
    this.init();
  },
  filters: {
    dateFormat,
  },
  methods: {
    async init() {
      let res = await getCommentsInfo(this.id, { pageindex: 1 });
      // console.log(this.$route.params.id);
      // console.log(res);
      this.comments = res.data.message;
    },
    async publish() {
      if (this.message.trim().length === 0) {
        return false;
      }
      // console.log(this.message);
      let res = await publishComment(this.id, {
        content: this.message,
      });
      // console.log(res);
      if (res.data.message === "评论提交成功") {
        this.$toast.success("发表成功");
        this.init();
        this.message = "";
      }
    },
  },
};
</script>

<style lang="less" scoped>
.putin {
  padding: 0px 10px;
  height: 50px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid #ccc;
  .right {
    padding-right: 70px;
    color: #5c81ff;
  }
}
.van-cell {
  border: 1px solid #ccc;
  width: 341 / 375 * 100vw;
  margin: 20px 16px;
  height: 100px;
}
.van-button {
  width: 341 / 375 * 100vw;
  margin: 0 16px;
}

.list {
  padding: 0px 10px;
  height: 50px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid #ccc;
  border-top: 1px solid #ccc;
  margin-top: 20px;
  .right {
    padding-right: 70px;
    color: #7a7a7a;
    font-size: 14px;
  }
}
.box {
  border-bottom: 1px solid #ccc;
  border-top: 1px solid #ccc;
  margin-top: 20px;
  .content {
    font-size: 16px;
    padding-left: 10px;
    color: #aeaeae;
    display: block;
    margin-top: 10px;
  }
  .news {
    padding: 0px 10px;
    height: 50px;
    .news_author {
      display: flex;
      justify-content: space-between;
      align-items: center;
      p {
        color: #189eff;
      }
      .right {
        // padding-right: 70px;
        color: #7a7a7a;
        font-size: 14px;
      }
    }
  }
}
</style>